package com.example.androidproject;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.WindowManager;

import androidx.annotation.Nullable;
public class MyView extends androidx.appcompat.widget.AppCompatImageView {
    public MyView(Context context) {
        super(context);
    }
    //获取屏幕的宽和高。根据屏幕的宽和高来算取景框的位置
    private int width, height;
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        width = wm.getDefaultDisplay().getWidth();
        height = wm.getDefaultDisplay().getHeight();
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setAlpha(250);
        // 下面是取景框的8条线
        // xy的算法是：把屏幕横着(逆时针旋转90度的屏幕)，从左到右是x轴，从上到下是y轴
        canvas.drawLine(200, 150, 300, 150, paint);
        canvas.drawLine(200, 150, 200, 200, paint);

        canvas.drawLine(200, height-150, 300, height-150, paint);
        canvas.drawLine(200, height-150, 200, height-200, paint);

        canvas.drawLine(width-355, 150, width-455, 150, paint);
        canvas.drawLine(width-355, 150, width-355, 200, paint);

        canvas.drawLine(width-355, height-150, width-455, height-150, paint);
        canvas.drawLine(width-355, height-150, width-355, height-200, paint);
        super.onDraw(canvas);
    }
}
